package com.coy.web.common.dto;


import com.alibaba.fastjson.annotation.JSONField;
import com.coy.web.common.exception.BizResultCode;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页响应DTO，主要用于不同服务之间的数据传递
 * 建议用法：必须指定业务数据T的具体类型，如： PageResult<UserInfoDto>
 *
 * @author chenck
 * @date 2020/8/20 9:42
 */
@Data
@Accessors(chain = true)
@ApiModel("分页请求基础出参")
public class PageResult<T> implements Serializable {
    /**
     * 返回码
     */
    @ApiModelProperty(value = "响应业务状态", position = 1)
    private String retcode = BizResultCode.SUCC.getCode();

    /**
     * 描述信息
     */
    @ApiModelProperty(value = "响应消息", position = 2)
    private String retmsg = BizResultCode.SUCC.getMsg();

    /**
     * 页开始下标 默认1
     */
    @ApiModelProperty(value = "页开始下标 默认1", position = 3)
    private Long pageNum = 1L;

    /**
     * 每页大小 默认10
     */
    @ApiModelProperty(value = "每页大小 默认10", required = true, position = 4)
    private Long pageSize = 10L;

    /**
     * 总记录数
     */
    @ApiModelProperty(value = "总记录数", position = 5)
    private Long total = 0L;

    /**
     * 总页数
     */
    @ApiModelProperty(value = "总页数", position = 6)
    private Long pages = 0L;

    /**
     * 返回业务数据
     */
    @ApiModelProperty(value = "返回业务数据", position = 7)
    private List<T> dataList = new ArrayList<>();

    /**
     * 附加数据
     * 如：可以存放统计维度数据等
     */
    @ApiModelProperty(value = "附加数据", position = 8)
    private Object attach;

    // 定义构造函数，方便使用
    public PageResult() {
    }

    public PageResult(String retmsg) {
        this.retmsg = retmsg;
    }

    public PageResult(List<T> dataList) {
        this.dataList = dataList;
    }

    public PageResult(String retcode, String retmsg) {
        this.retcode = retcode;
        this.retmsg = retmsg;
    }

    public PageResult(String retcode, String retmsg, Long pageNum, Long pageSize, Long total) {
        this.retcode = retcode;
        this.retmsg = retmsg;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
    }

    /**
     * 校验是否成功
     */
    @JSONField(serialize = false)
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    public boolean isSucc() {
        return BizResultCode.SUCC.getCode().equals(retcode);
    }

    private static <T> PageResult<T> result(List<T> dataList, String retcode, String retmsg) {
        return new PageResult().setRetcode(retcode).setRetmsg(retmsg).setDataList(dataList);
    }

    private static <T> PageResult<T> result(List<T> dataList, String retcode, String retmsg, Long pageNum, Long pageSize, Long total, Long pages) {
        return new PageResult().setRetcode(retcode).setRetmsg(retmsg)
                .setDataList(dataList)
                .setPageNum(pageNum)
                .setPageSize(pageSize)
                .setTotal(total)
                .setPages(pages);
    }

    /**
     * 成功设置
     */
    public static PageResult succ() {
        return PageResult.result(null, BizResultCode.SUCC.getCode(), BizResultCode.SUCC.getMsg());
    }

    public static PageResult succ(String retmsg) {
        return PageResult.result(null, BizResultCode.SUCC.getCode(), retmsg);
    }

    public static <T> PageResult<T> succ(List<T> dataList) {
        return PageResult.result(dataList, BizResultCode.SUCC.getCode(), BizResultCode.SUCC.getMsg());
    }

    public static <T> PageResult<T> succ(List<T> dataList, String retmsg) {
        return PageResult.result(dataList, BizResultCode.SUCC.getCode(), retmsg);
    }

    public static <T> PageResult<T> succ(List<T> dataList, String retcode, String retmsg) {
        return PageResult.result(dataList, retcode, retmsg);
    }

    public static <T> PageResult<T> succ(List<T> dataList, Long pageNum, Long pageSize, Long total, Long pages) {
        return PageResult.result(dataList, BizResultCode.SUCC.getCode(), BizResultCode.SUCC.getMsg(), pageNum, pageSize, total, pages);
    }

    public static <T> PageResult<T> succ(List<T> dataList, Long pageNum, Long pageSize, Long total, Long pages, String retmsg) {
        return PageResult.result(dataList, BizResultCode.SUCC.getCode(), retmsg, pageNum, pageSize, total, pages);
    }

    /**
     * 失败设置
     */
    public static PageResult error(String retmsg) {
        return PageResult.result(null, BizResultCode.ERR_SYSTEM.getCode(), retmsg);
    }

    public static PageResult error(String retcode, String retmsg) {
        return PageResult.result(null, retcode, retmsg);
    }

    public static <T> PageResult<T> error(List<T> dataList) {
        return PageResult.result(dataList, BizResultCode.ERR_SYSTEM.getCode(), BizResultCode.ERR_SYSTEM.getMsg());
    }

    public static <T> PageResult<T> error(List<T> dataList, String retmsg) {
        return PageResult.result(dataList, BizResultCode.ERR_SYSTEM.getCode(), retmsg);
    }

    public static <T> PageResult<T> error(List<T> dataList, String retcode, String retmsg) {
        return PageResult.result(dataList, retcode, retmsg);
    }

}
